Imports AUtility

Partial Public Class ZadnjiRezultati
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'ALog.ConnectionString = GlobalTenisLiga.ConnectionString
        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "_Load START", "", "", "")

        Dim zadnjiRezultat As String = "ZadnjiRezultati"
        Dim zadnjiRezultatP As String = "ZadnjiRezultatiP"
        If (Request.QueryString("LigaId") IsNot Nothing) Then
            'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "_LigaId", "", "", "")

            zadnjiRezultat = String.Format("ZadnjiRezultati{0}_{1}", Request.QueryString("SezonaId"), Request.QueryString("LigaId"))
            lblPrijateljske.Visible = False
            Label3.Visible = False
        Else
            lblTablica.Visible = False
        End If
        'If (Application(zadnjiRezultat) Is Nothing Or Application(zadnjiRezultatP) Is Nothing) Then
        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "ZadnjiRezultat", "", "", "")

        Label1.Text = ""

        'Dim selectStr As String = String.Format("SELECT TOP 20 ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM Susret INNER JOIN SusretIgrac ON SusretIgrac.SusretId = Susret.SusretId INNER JOIN vIgrac on SusretIgrac.IgracId = vIgrac.IgracId ORDER BY Datum DESC, SusretIgracId")
        Dim selectStr As String = String.Format("SELECT TOP 20 LigaNaziv, ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM vSusreti ORDER BY Datum DESC, SusretId DESC, Pobjednik DESC")
        If (Request.QueryString("LigaId") IsNot Nothing) Then
            'selectStr = String.Format("SELECT TOP 20 ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM Susret INNER JOIN SusretIgrac ON SusretIgrac.SusretId = Susret.SusretId INNER JOIN vIgrac on SusretIgrac.IgracId = vIgrac.IgracId WHERE LigaId = {0} AND SezonaId = {1} ORDER BY Datum DESC, SusretIgracId", Request.QueryString("LigaId"), Request.QueryString("LigaId"))
            selectStr = String.Format("SELECT TOP 20 LigaNaziv, ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM vSusreti WHERE LigaId = {0} AND SezonaId = {1} ORDER BY Datum DESC, SusretId DESC, Pobjednik DESC", Request.QueryString("LigaId"), Request.QueryString("SezonaId"))

            TablicaOdigranih()

        End If
        Dim resultsDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        gvRezultati.DataSource = resultsDT
        gvRezultati.DataBind()

        'Label1.Text = resultsDT.Rows.Count.ToString()
        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "For Pre", "", "", "")

        For i As Integer = 0 To resultsDT.Rows.Count - 1 Step 2
            Label1.Text = Label1.Text + "<br>" + Result(resultsDT, i)
        Next
        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "For Post", "", "", "")

        Application(zadnjiRezultat) = Label1.Text

        selectStr = String.Format("SELECT TOP 26 ImePrezime, Set1, Set2, Set3, convert(varchar,Datum,104) AS Datum2, Lokacija FROM vSusretiPrijateljski ORDER BY Datum DESC, SusretId DESC, Pobjednik DESC")
        resultsDT = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)

        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "For2 Pre", "", "", "")

        lblPrijateljske.Text = ""
        For i As Integer = 0 To resultsDT.Rows.Count - 1 Step 2
            lblPrijateljske.Text = lblPrijateljske.Text + "<br>" + Result(resultsDT, i)
        Next
        Application(zadnjiRezultatP) = lblPrijateljske.Text
        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "For2 Post", "", "", "")


        'Else
        ''ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "Else1", "", "", "")

        'Label1.Text = Application(zadnjiRezultat).ToString()
        'lblPrijateljske.Text = Application(zadnjiRezultatP).ToString()
        ''ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "Else2", "", "", "")

        'End If



    End Sub


    Private Sub TablicaOdigranih()

        Dim tblO As String = "<table border=1>"
        Dim selectIgraci As String = String.Format("SELECT DISTINCT IgracId, ImePrezime FROM vSusreti WHERE LigaId = {0} AND SezonaId = {1} ORDER BY IgracId", Request.QueryString("LigaId"), Request.QueryString("SezonaId"))
        Dim igraciDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectIgraci)

        Dim rowStr As String = "<tr><td></td>"

        For Each row As DataRow In igraciDT.Rows
            rowStr += String.Format("<td>{0}</td>", row("ImePrezime"))
        Next

        'Gray cells
        Dim i As Integer = 1
        For iRow As Integer = 0 To igraciDT.Rows.Count - 2
            rowStr += String.Format("<tr><td>{0}</td>", igraciDT.Rows(iRow)("ImePrezime"))
            Dim j As Integer = 0
            For j = 0 To i - 1
                rowStr += String.Format("<td bgcolor=c0c0c0></td>")
            Next
            'Try
            For iigrac As Integer = 1 To igraciDT.Rows.Count - i
                Dim res As String = Results(igraciDT.Rows(iRow)("IgracId"), igraciDT.Rows(iRow + iigrac)("IgracId"), Request.QueryString("SezonaId"))
                rowStr += String.Format("<td>{0}</td>", res)
            Next
            'Catch ex As Exception
            'rowStr += ex.Message
            'End Try

            i = i + 1
        Next

        'Result cells


        rowStr += String.Format("</tr>")


        tblO = String.Format("<table border=1>{0}</table>", rowStr)

        lblTablica.Text = tblO
    End Sub

    Private Function Results(ByVal igrac1 As Object, ByVal igrac2 As Object, ByVal sezonaId As Object) As String

        Dim selectStr As String = String.Format("SELECT IgracId, Set1, Set2, Set3 FROM vSusreti WHERE SusretId IN ( SELECT SusretId FROM vSusreti WHERE IgracId = {0} AND SusretId IN ( SELECT SusretId FROM vSusreti WHERE IgracId = {1} ) AND SezonaId = {2} ) ORDER BY IgracId", igrac1, igrac2, sezonaId)
        Dim resultDT As DataTable = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)

        If (resultDT.Rows.Count > 0) Then
            selectStr = String.Format("{0}:{1}, {2}:{3}", resultDT.Rows(0)("Set1"), resultDT.Rows(1)("Set1"), resultDT.Rows(0)("Set2"), resultDT.Rows(1)("Set2"))
            If (Not resultDT.Rows(0).IsNull("Set3")) Then
                selectStr = String.Format("{0}:{1}, {2}:{3}, {4}:{5}", resultDT.Rows(0)("Set1"), resultDT.Rows(1)("Set1"), resultDT.Rows(0)("Set2"), resultDT.Rows(1)("Set2"), resultDT.Rows(0)("Set3"), resultDT.Rows(1)("Set3"))
            End If
        Else
            selectStr = ""
        End If

        Return selectStr

    End Function

    Private Function Result(ByVal resultDT As DataTable, ByVal rowIndex As Integer) As String
        Dim resultTable As String = ""
        If (resultDT.Columns.Contains("LigaNaziv")) Then
            If (resultDT.Rows(rowIndex + 1).IsNull("Set3")) Then
                resultTable = String.Format("<table width=150><tr><td colspan=4 align=center  bgcolor=#F0F0F0>{0}</td></tr><tr><td colspan=4 align=center>{1} - {2}</td></tr><tr bgcolor=#E0E0E0><td>{3}</td><td>{4}</td><td>{5}</td></tr><tr><td>{6}</td><td>{7}</td><td>{8}</td></tr></table>", resultDT.Rows(rowIndex)("LigaNaziv"), resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"))
            Else
                resultTable = String.Format("<table width=150><tr><td colspan=4 align=center  bgcolor=#F0F0F0>{0}</td></tr><tr><td colspan=4 align=center>{1} - {2}</td></tr><tr bgcolor=#E0E0E0><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr><tr><td>{7}</td><td>{8}</td><td>{9}</td><td>{10}</td></tr></table>", resultDT.Rows(rowIndex)("LigaNaziv"), resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex)("Set3"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"), resultDT.Rows(rowIndex + 1)("Set3"))
            End If
        Else
            If (resultDT.Rows(rowIndex + 1).IsNull("Set3")) Then
                resultTable = String.Format("<table width=150><tr><td colspan=4 align=center>{0} - {1}</td></tr><tr bgcolor=#E0E0E0><td>{2}</td><td>{3}</td><td>{4}</td></tr><tr><td>{5}</td><td>{6}</td><td>{7}</td></tr></table>", resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"))
            Else
                resultTable = String.Format("<table width=150><tr><td colspan=4 align=center>{0} - {1}</td></tr><tr bgcolor=#E0E0E0><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr><tr><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr></table>", resultDT.Rows(rowIndex)("Datum2"), resultDT.Rows(rowIndex)("Lokacija"), resultDT.Rows(rowIndex)("ImePrezime"), resultDT.Rows(rowIndex)("Set1"), resultDT.Rows(rowIndex)("Set2"), resultDT.Rows(rowIndex)("Set3"), resultDT.Rows(rowIndex + 1)("ImePrezime"), resultDT.Rows(rowIndex + 1)("Set1"), resultDT.Rows(rowIndex + 1)("Set2"), resultDT.Rows(rowIndex + 1)("Set3"))
            End If

        End If


            Return resultTable
    End Function



End Class